package org.korifey.ffbdd.samples.queens;

import java.util.Arrays;

public class Position {
	int [] vert;
	int current;
	
	public Position(int [] vert, int cur) {
		this.vert = vert;
		current = cur;
	}
	
	public Position(int n) {
		vert = new int[n];
		current = 0;
	}
	
	public Position add(int queenHorPosition) {
		for (int i=0; i<current; i++) {
			if (vert[i] == queenHorPosition || current - i == Math.abs(queenHorPosition - vert[i])) {
				return null;
			}
		}
		int[] v = vert.clone();
		v[current] = queenHorPosition;
		return new Position(v, current + 1);
	}
	
	public int getSize() {
		return current;
	}
	
	public String print(boolean verbose) {
		if (!verbose) return Arrays.toString(vert);
		else {
			StringBuffer res = new StringBuffer(1024);
			for (int i=0; i<vert.length; i++) {
				for (int j=0; j<current; j++) {
					if (vert[j] == i) res.append("X");
					else res.append("-");
				}
				res.append("\r\n");
			}
			return res.toString();
		}
	}
}
